import numpy as np
import cv2 as cv
img = cv.imread('../../data/multObj.png')

imgray =cv.cvtColor(img,cv.COLOR_BGR2GRAY)
ret,thresh=cv.threshold(imgray,127,255,0)
retrievalMode =cv.RETR_TREE
approxMethod=cv.CHAIN_APPROX_NONE
contours,hierarchy=cv.findContours(thresh,retrievalMode,approxMethod)



SymbolPerimeter=40
offset = round(SymbolPerimeter*0.2)
validIdx=[]
for idx,cnt in enumerate(contours):
  perimeter =cv.arcLength(cnt,True)
  print(idx,":",cnt.size,perimeter)
  minP =SymbolPerimeter -offset
  maxP =SymbolPerimeter +offset
  if perimeter >minP and perimeter <maxP:
    validIdx.append(idx)

imgBackGround =np.zeros(img.shape,np.uint8)

for idx in validIdx:
      cv.drawContours(imgBackGround,contours,idx,(0,0,255),1)
cv.imshow("img",img)
cv.imshow("imgBG",imgBackGround)
cv.waitKey(0)
cv.destroyAllWindows()